package com.library.controller;


import java.util.List;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.library.dto.pojo.PageBean;
import com.library.dto.vo.ResultsReader;
import com.library.entity.Lesson;
import com.library.entity.Questions;
import com.library.entity.Reader;
import com.library.entity.Results;
import com.library.mapper.ResultsReaderMapper;
import com.library.service.ILessonService;
import com.library.service.IResultsService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Controller
@SessionAttributes(value= {"lessons","adminResults","readerResults","reader","admin"})
@RequestMapping("/results")
public class ResultsController {
	@Autowired
	private IResultsService resultServiceImpl;
	@Autowired
	private ILessonService lessonServiceImpl;
	@Autowired
	private ResultsReaderMapper resultsReaderMapper;
	
	/**管理员成绩查询的初始页面
	 * @param model
	 * @return
	 */
	@RequestMapping("admin/selectResults")
	public String selectResults(ModelMap model) {
		List<Lesson> lessons=lessonServiceImpl.selectList(new EntityWrapper<Lesson>());
		model.addAttribute("lessons", lessons);
		return "admin/adminResults";
	}
	
	/**管理员成绩查询按钮,保存查询条件
	 * @param model
	 * @param adminResults
	 * @return
	 */
	@RequestMapping("admin/selectResultsInfo")
	public String selectResultsInfo(ModelMap model,Results adminResults) {
		System.out.println(adminResults);
		int pageSize = 8;
		List<ResultsReader> list=null;
		int pageNum=1;
		//保存查询条件的sesssion
		model.addAttribute("adminResults", adminResults);
		//创建条件构造器
		Wrapper<ResultsReader> wrapper=new EntityWrapper<ResultsReader>();
		
		
			wrapper=wrapper.eq("lessonId",adminResults.getLessonId());
			if (adminResults.getDate()!="") {
				wrapper=wrapper.eq("date", adminResults.getDate());
			}
			if (adminResults.getReaderId()!=null) {
				wrapper=wrapper.eq("readerId", adminResults.getReaderId());
			}
			//分页查找
			int count = resultsReaderMapper.selectCount(wrapper);
			PageBean<ResultsReader> pageBean = new PageBean<ResultsReader>(pageNum, pageSize, count);
			if (pageNum > pageBean.getTotalPage()) {
				pageNum = pageBean.getTotalPage();
				pageBean.setPageNum(pageNum);
			}
			if (pageNum<=0) {
				pageBean.setPageNum(1);
			}
			list=resultsReaderMapper.selectPage(new Page<ResultsReader>(pageNum, pageSize), wrapper);
			pageBean.setList(list);
			model.addAttribute("bean", pageBean);
		
		return "admin/adminResults";
	}
	
	/**管理员成绩分页
	 * @param model
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("admin/selectResultsInfo/{pageNum}")
	public String selectResultsInfoPage(ModelMap model,@PathVariable(value = "pageNum")int pageNum) {
		Results adminResults=(Results) model.get("adminResults");
		int pageSize = 8;
		List<ResultsReader> list=null;
		//保存查询条件的sesssion
		model.addAttribute("adminResults", adminResults);
		//创建条件构造器
		Wrapper<ResultsReader> wrapper=new EntityWrapper<ResultsReader>();
		
		
			wrapper=wrapper.eq("lessonId",adminResults.getLessonId());
			if (adminResults.getDate()!="") {
				wrapper=wrapper.eq("date", adminResults.getDate());
			}
			if (adminResults.getReaderId()!=null) {
				wrapper=wrapper.eq("readerId", adminResults.getReaderId());
			}
			//分页查找
			int count = resultsReaderMapper.selectCount(wrapper);
			PageBean<ResultsReader> pageBean = new PageBean<ResultsReader>(pageNum, pageSize, count);
			if (pageNum > pageBean.getTotalPage()) {
				pageNum = pageBean.getTotalPage();
				pageBean.setPageNum(pageNum);
			}
			if (pageNum<=0) {
				pageBean.setPageNum(1);
			}
			list=resultsReaderMapper.selectPage(new Page<ResultsReader>(pageNum, pageSize), wrapper);
			pageBean.setList(list);
			model.addAttribute("bean", pageBean);
		return "admin/adminResults";
	}
	
	/**读者成绩查询初始页面
	 * @param model
	 * @return
	 */
	@RequestMapping("reader/selectResults")
	public String selectResultsReader(ModelMap model) {
		List<Lesson> lessons=lessonServiceImpl.selectList(new EntityWrapper<Lesson>());
		model.addAttribute("lessons", lessons);
		return "reader/readerResults";
	}
	
	/**读者成绩查询按钮
	 * @param model
	 * @param readerResults
	 * @return
	 */
	@RequestMapping("reader/selectResultsInfo")
	public String selectResultsInfoReader(ModelMap model,Results readerResults) {
//		readerResults.setReaderId(151080127);//整合完毕后通过session获取登录的用户的readerId
		Reader reader=(Reader) model.get("reader");
		System.out.println(reader);
		readerResults.setReaderId(reader.getReaderId());
		int pageSize = 8;
		List<ResultsReader> list=null;
		int pageNum=1;
		//保存查询条件的sesssion
		model.addAttribute("readerResults", readerResults);
		//创建条件构造器
		Wrapper<ResultsReader> wrapper=new EntityWrapper<ResultsReader>();
			wrapper=wrapper.eq("lessonId",readerResults.getLessonId());
			if (readerResults.getDate()!="") {
				wrapper=wrapper.eq("date", readerResults.getDate());
			}
			if (readerResults.getReaderId()!=null) {
				wrapper=wrapper.eq("readerId", readerResults.getReaderId());
			}
			//分页查找
			int count = resultsReaderMapper.selectCount(wrapper);
			PageBean<ResultsReader> pageBean = new PageBean<ResultsReader>(pageNum, pageSize, count);
			if (pageNum > pageBean.getTotalPage()) {
				pageNum = pageBean.getTotalPage();
				pageBean.setPageNum(pageNum);
			}
			if (pageNum<=0) {
				pageBean.setPageNum(1);
			}
			list=resultsReaderMapper.selectPage(new Page<ResultsReader>(pageNum, pageSize), wrapper);
			pageBean.setList(list);
			model.addAttribute("bean", pageBean);
		return "reader/readerResults";
	}
	
	/**读者成绩查询分页
	 * @param model
	 * @param pageNum
	 * @return
	 */
	@RequestMapping("reader/selectResultsInfo/{pageNum}")
	public String selectResultsInfoPageReader(ModelMap model,@PathVariable(value = "pageNum")int pageNum) {
		Results adminResults=(Results) model.get("readerResults");
		adminResults.setReaderId(151080127);//整合完毕后通过session获取登录的用户的readerId
		int pageSize = 8;
		List<ResultsReader> list=null;
		//保存查询条件的sesssion
		model.addAttribute("adminResults", adminResults);
		//创建条件构造器
		Wrapper<ResultsReader> wrapper=new EntityWrapper<ResultsReader>();
		
		
			wrapper=wrapper.eq("lessonId",adminResults.getLessonId());
			if (adminResults.getDate()!="") {
				wrapper=wrapper.eq("date", adminResults.getDate());
			}
			if (adminResults.getReaderId()!=null) {
				wrapper=wrapper.eq("readerId", adminResults.getReaderId());
			}
			//分页查找
			int count = resultsReaderMapper.selectCount(wrapper);
			PageBean<ResultsReader> pageBean = new PageBean<ResultsReader>(pageNum, pageSize, count);
			if (pageNum > pageBean.getTotalPage()) {
				pageNum = pageBean.getTotalPage();
				pageBean.setPageNum(pageNum);
			}
			if (pageNum<=0) {
				pageBean.setPageNum(1);
			}
			list=resultsReaderMapper.selectPage(new Page<ResultsReader>(pageNum, pageSize), wrapper);
			pageBean.setList(list);
			model.addAttribute("bean", pageBean);
		return "reader/readerResults";
	}
}

